package com.wish.wishsystem_management.service.impl;

import com.wish.wishsystem_management.entity.SysRole;
import com.wish.wishsystem_management.entity.SysUserRole;
import com.wish.wishsystem_management.mapper.SysUserRoleMapper;
import com.wish.wishsystem_management.service.ISysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户与角色对应关系 服务实现类
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {


    @Override
    public void saveOrUpdate2(Long userId, List<Long> roleIdList) {
        //先删除用户与角色关系

        baseMapper.delete2(userId);

        //保存用户与角色关系
        for (Long rr : roleIdList
        ) {
            baseMapper.add2(userId, rr);
        }


    }

    @Override
    public List<Long> queryUserIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }


    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        if (roleIdList.size() == 0) {
            return;
        }

        //先删除用户与角色关系
        baseMapper.delete(userId);

        //保存用户与角色关系
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("roleIdList", roleIdList);
        baseMapper.save(map);
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        baseMapper.deleteBatch(userIds);
    }
}
